﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using SomaiNordeste.FabricaRacao.DomainModel.Repository;
using SomaiNordeste.FabricaRacao.DomainModel;

namespace SomaiNordeste.FabricaRacao.Presentation
{
    public partial class frmBatidasEdicao : Form
    {
        private BatidaRepository _batidaRepository;
        private Batida _batida;

        public frmBatidasEdicao()
        {
            InitializeComponent();
            _batidaRepository = new BatidaRepository();
            _batida = new Batida();
        }

        private void frmBatidasEdicao_Load(object sender, EventArgs e)
        {
            CarregaRacoes(date.Value);
        }

        private void CarregaRacoes(DateTime dateTime)
        {
            cmbRacao.DataSource = _batidaRepository.FindBy(dateTime);
            cmbRacao.DisplayMember = "Descricao";
        }

        private void date_ValueChanged(object sender, EventArgs e)
        {
            CarregaRacoes(date.Value);
        }

        private void cmbRacao_SelectedIndexChanged(object sender, EventArgs e)
        {
            Batida batida = new Batida();

            batida = ((Batida)cmbRacao.SelectedValue);
            if (cmbRacao.SelectedIndex > 0)
                grdRacoes.DataSource = _batidaRepository.FindBy(batida, date.Value).DefaultView;
        }

        private void grdRacoes_Click(object sender, EventArgs e)
        {


            if (grdRacoes.CurrentRow == null || string.IsNullOrEmpty(
                grdRacoes.Rows[grdRacoes.CurrentRow.Index].Cells[0].Value.ToString()))
            {
                txtPeso.Text = string.Empty;
                lblRacao.Text = string.Empty;
                return;
            }

            _batida = new Batida();
            _batida.Date = DateTime.Parse(grdRacoes.Rows[grdRacoes.CurrentRow.Index].Cells[0].Value.ToString());
            _batida.Descricao = grdRacoes.Rows[grdRacoes.CurrentRow.Index].Cells[1].Value.ToString();
            _batida.BatidaN = int.Parse(grdRacoes.Rows[grdRacoes.CurrentRow.Index].Cells[2].Value.ToString());
            _batida.KG_Total = double.Parse(grdRacoes.Rows[grdRacoes.CurrentRow.Index].Cells[3].Value.ToString());

            lblRacao.Visible = true;
            lblRacao.Text = _batida.Descricao;
            txtPeso.Text = _batida.KG_Total.ToString();
        }

        private void btnAlterar_Click(object sender, EventArgs e)
        {
            _batida.KG_Total =double.Parse(txtPeso.Text);
            _batidaRepository.Update(_batida);
            MessageBox.Show("Peso alterado com sucesso!");


            Batida batida = new Batida();
            batida = ((Batida)cmbRacao.SelectedValue);
            if (cmbRacao.SelectedIndex > 0)
                grdRacoes.DataSource = _batidaRepository.FindBy(batida, date.Value).DefaultView;
        }

        private void date_Enter(object sender, EventArgs e)
        {
            
        }

        private void date_Leave(object sender, EventArgs e)
        {

        }

        private void cmbRacao_Enter(object sender, EventArgs e)
        {
            cmbRacao.BackColor = Color.Yellow;
        }

        private void cmbRacao_Leave(object sender, EventArgs e)
        {
            cmbRacao.BackColor = Color.White;
        }

        private void txtPeso_Enter(object sender, EventArgs e)
        {
            txtPeso.BackColor = Color.Yellow;
        }

        private void txtPeso_Leave(object sender, EventArgs e)
        {
            txtPeso.BackColor = Color.White;
        }

    }
}
